package com.yohann.order_service.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yohann.order_service.client.CourseClient;
import com.yohann.order_service.client.UserClient;
import com.yohann.order_service.entity.Course;
import com.yohann.order_service.entity.Orders;
import com.yohann.order_service.entity.User;
import com.yohann.order_service.entity.vo.OrderVo;
import com.yohann.order_service.mapper.OrdersMapper;
import com.yohann.order_service.service.OrdersService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yohann.order_service.utils.OrderIdUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * <p>
 * 订单表 服务实现类
 * </p>
 *
 * @author Yohann
 * @since 2020-11-09
 */
@Service
public class OrdersServiceImpl extends ServiceImpl<OrdersMapper, Orders> implements OrdersService {

    @Autowired
    private CourseClient courseClient;

    @Autowired
    private UserClient userClient;

    /**
     * 查询订单信息
     *
     * @param current
     * @param limit
     * @param order
     * @return
     */
    @Override
    public Map<String, Object> queryOrder(int current, int limit, Orders order) {
        LambdaQueryWrapper<Orders> orderWrapper = null;

        if (order != null) {
            List<Integer> courseIds = null;
            if (order.getTransactionId() != null) {
                //模糊查询课程 得到id集合
                courseIds = courseClient.searchCourse(order.getTransactionId());
                courseIds.add(0);
            }

            //在订单中找到对应的集合
            orderWrapper = new LambdaQueryWrapper<>();
            if (order.getState() != null) {
                orderWrapper.eq(Orders::getState, order.getState());
            }
            if (courseIds != null) {
                orderWrapper.in(Orders::getCourseId, courseIds);
            }
        }

        //分页查询
        Page<Orders> page = new Page<>(current, limit);
        baseMapper.selectPage(page, orderWrapper);

        //订单视图信息集合
        ArrayList<OrderVo> orderVos = new ArrayList<>();
        for (Orders record : page.getRecords()) {
            OrderVo orderVo = new OrderVo();
            orderVos.add(orderVo);

            Course course = courseClient.getCourse(record.getCourseId());
            User user = userClient.getUser(record.getUserId());

            BeanUtils.copyProperties(record, orderVo);
            orderVo.setCourseName(course.getTitle());
            orderVo.setUserName(user.getNickname());
        }

        //将结果存入map中并返回
        Map<String, Object> map = new HashMap<>();
        map.put("total", page.getTotal());
        map.put("rows", orderVos);
        return map;
    }

    /**
     * 获取订单信息
     *
     * @param id
     * @return
     */
    @Override
    public OrderVo getOrderInfo(String id) {
        return baseMapper.getOrderInfo(id);
    }

    /**
     * 获取用户的所有订单
     *
     * @param id
     * @return
     */
    @Override
    public List<OrderVo> getUserOrder(int id) {
        return baseMapper.getUserOrder(id);
    }

    @Override
    public boolean getPurchaseStatus(int userId, int courseId) {
        LambdaQueryWrapper<Orders> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Orders::getUserId, userId);
        wrapper.eq(Orders::getCourseId, courseId);
        Integer count = baseMapper.selectCount(wrapper);

        return count > 0;
    }

    @Override
    public void pay(Orders order) {
        LambdaQueryWrapper<Orders> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Orders::getCourseId, order.getCourseId())
                .eq(Orders::getUserId, order.getUserId());

        if (count(wrapper) > 0) {
            return;
        }

        //生成订单id
        order.setId(OrderIdUtil.getOrderNo());
        //设置创建时间
        order.setGmtCreate(new Date());
        order.setPayTime(new Date());
        order.setTransactionId(System.currentTimeMillis() + "");
        order.setState(true);
        save(order);
    }
}
